package com.hzlx.service;

import com.hzlx.bean.Permission;
import com.hzlx.dao.PermissionDao;
import com.hzlx.dto.MenuDto;
import com.hzlx.dto.MenuItemDto;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PermissionService {
    @Resource
    PermissionDao permissionDao;
    public List<MenuDto> findMenus(String account){
        // 1. 查询顶级菜单
        List<Permission> menus = permissionDao.findAllRoutesByUser(account);
        // 2. 查询每个顶级菜单的下级菜单
        return menus.stream().filter(perm->perm.getPermType()==0).distinct().map(perm ->{
                        // Permission->MenuDto
                        MenuDto menuDto = MenuDto.builder()
                        .id(perm.getId())
                        .title(perm.getShowName())
                        .build();
                        // 下一级
                        List<MenuItemDto> subs = menus.stream().filter(p->p.getParentId() == menuDto.getId())
                                .distinct().map(p->MenuItemDto.builder()
                                        .page(p.getPage())
                                        .title(p.getShowName())
                                        .build()).collect(Collectors.toList());
                        menuDto.setMenuItem(subs);
                        return menuDto;
                })
                .collect(Collectors.toList());
    }
}
